﻿#region [ Using ] 

using System;
using System.Threading;
using System.Windows.Forms;
using GZipStreamTool.Base.Core;
using GZipStreamTool.Base.GZipTools;

#endregion

namespace GZipStreamTool.UI
{
    /// <summary>
    ///     GZipStreamToolUI User Interface Class
    /// </summary>
    public partial class GZipStreamToolUI : Form
    {
        //endregion 

        //region [ Constructors ]
        /// <summary>
        ///     GZipStreamToolUI
        /// </summary>
        public GZipStreamToolUI()
        {
            InitializeComponent();
        }

        //endregion

        //region [ EventHandlers ]
        /// <summary>
        ///     textBoxFilePath_TextChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxFilePath_TextChanged(object sender, object e)
        {
            watchtool = new GZipStreamToolWatch();
        }

        /// <summary>
        ///     buttonBrowse_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonBrowse_Click(object sender, object e)
        {
            watchtool = new GZipStreamToolWatch();
            openFileDialog = new OpenFileDialog();
            openFileDialog.ShowDialog();
            if (openFileDialog.CheckPathExists)
            {
                textBoxFilePath.Text = openFileDialog.FileName;
                if (textBoxFilePath.Text.Replace(".gz", "") != textBoxFilePath.Text)
                {
                    //  this.buttonCompress.Enabled = false;
                    //  this.buttonDecompress.Enabled = true;
                }
            }
        }


        /// <summary>
        ///     buttonCompress_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCompress_Click(object sender, object e)
        {
            watchtool = new GZipStreamToolWatch();

            var filename = textBoxFilePath.Text;

            //this.buttonDecompress.Enabled = false;

            gzt = new GZipTool(filename);

            memoryConsumption = (MemoryConsumption) comboBoxMemoryUsage.SelectedItem;

            gzt.MemConsumption = memoryConsumption;
            gzt.Action = ActionType.Compress;


            // file readout 
            gzt.FileReadOutBegins += Bbw_FileReadOutBegins;
            gzt.FileReadOutCompleted += Bbw_FileReadOutCompleted;

            // file action
            gzt.FileActionBegins += Bbw_FileActionBegins;
            gzt.FileActionCompleted += Bbw_FileActionCompleted;

            // file write out
            gzt.FileSavingBegins += Bbw_FileSavingBegins;
            gzt.FileSavingCompleted += Bbw_FileSavingCompleted;

            // file action   
            worker = new Thread(gzt.Compress);
            worker.Start();
        }

        private void Bbw_FileSavingCompleted(object e, object o)
        {
            watchtool.Action(WatchType.Writeout, WatchAction.Stop);
            watchtool.Action(WatchType.Total, WatchAction.Stop);

            gzt.Dispose();
        }

        private void Bbw_FileSavingBegins(object e, object o)
        {
            watchtool.Action(WatchType.Writeout, WatchAction.Start);
        }

        private void Bbw_FileActionCompleted(object e, object o)
        {
            watchtool.Action(WatchType.Action, WatchAction.Stop);
        }

        private void Bbw_FileActionBegins(object e, object o)
        {
            watchtool.Action(WatchType.Action, WatchAction.Start);
        }

        private void Bbw_FileReadOutBegins(object e, object o)
        {
            //  this.labelCompressLength.Text = gzt.Reader.ReaderFileStream.Length.ToString();
            //  this.labelDecompressLength.Text = gzt.Writer.WriterFileStream.Length.ToString();


            watchtool.Action(WatchType.Total, WatchAction.Start);
            watchtool.Action(WatchType.Readout, WatchAction.Start);
        }

        private void Bbw_FileReadOutCompleted(object e, object o)
        {
            watchtool.Action(WatchType.Readout, WatchAction.Stop);
        }

        /// <summary>
        ///     buttonDecompress_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonDecompress_Click(object sender, object e)
        {
            watchtool = new GZipStreamToolWatch();

            var filename = textBoxFilePath.Text;

            // this.buttonCompress.Enabled = false;

            gzt = new GZipTool(filename);

            memoryConsumption = (MemoryConsumption) comboBoxMemoryUsage.SelectedItem;

            gzt.MemConsumption = memoryConsumption;
            gzt.Action = ActionType.Decompress;


            // file readout 
            gzt.FileReadOutBegins += Bbw_FileReadOutBegins;
            gzt.FileReadOutCompleted += Bbw_FileReadOutCompleted;

            // file action
            gzt.FileActionBegins += Bbw_FileActionBegins;
            gzt.FileActionCompleted += Bbw_FileActionCompleted;

            // file write out
            gzt.FileSavingBegins += Bbw_FileSavingBegins;
            gzt.FileSavingCompleted += Bbw_FileSavingCompleted;

            // file action   
            worker = new Thread(gzt.Decompress);
            worker.Start();
        }

        /// <summary>
        ///     buttonCancel_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCancel_Click(object sender, object e)
        {
            watchtool = new GZipStreamToolWatch();
            gzt.Action = ActionType.Cancel;
            if (worker.IsAlive) worker.Abort();
            gzt.Dispose();
            //gzip.Dispose();
        }

        /// <summary>
        ///     internalTimer_Tick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void internalTimer_Tick(object sender, object e)
        {
            internalTimerReadoutLabel.Text = watchtool.ReadoutWatch;
            internalTimerActionLabel.Text = watchtool.ActionWatch;
            internalTimerWriteOutLabel.Text = watchtool.WriteoutWatch;
            internalTimerTotalLabel.Text = watchtool.TotalWatch;
        }


        private void internalTimerLabel_Click(object sender, object e)
        {
        }

        private void timer1_Tick(object sender, object e)
        {
        }

        private void comboBoxMemoryUsage_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void GZipStreamToolUI_Shown(object sender, EventArgs e)
        {
            comboBoxMemoryUsage.DataSource = Enum.GetValues(typeof(MemoryConsumption));
        }

        private void GZipStreamToolUI_Load(object sender, EventArgs e)
        {
        }

        //endregion

        #region [ Members ]

        private GZipStreamToolWatch watchtool = new GZipStreamToolWatch();
        private GZipTool gzt;
        private Thread worker;
        private MemoryConsumption memoryConsumption;

        #endregion
    }
}